//
//  LSInfoSelectController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2019/1/11.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSInfoSelectController.h"

@interface LSInfoSelectController () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSArray *pickerDataArray;
@end

@implementation LSInfoSelectController

- (instancetype)initWithDatePickerView {
    self = [super init];
    if (self) {
        self.datePicker = [[UIDatePicker alloc] init];
        // 2.设置模式
        // self.datePicker.date = self.date;
        NSDate* maxDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
        self.datePicker.maximumDate = maxDate;
        self.datePicker.datePickerMode = UIDatePickerModeDate;
        [self.view addSubview:_datePicker];
        //设置frame
        __weak typeof(self) weakSelf = self;
        
        [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf.view.mas_centerX);
            make.centerY.equalTo(weakSelf.view.mas_centerY).with.offset(-40);
            make.width.mas_equalTo(290);
        }];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self.view addSubview:self.pickerView];
        //设置frame
        __weak typeof(self) weakSelf = self;
        
        [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf.view.mas_centerX);
            make.centerY.equalTo(weakSelf.view.mas_centerY).with.offset(-40);
            make.width.mas_equalTo(ScreenWidth-30);
        }];
    }
    return self;
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.pickerDataArray.count;
    } else if (component == 1) {
        NSInteger firstSelectedRow = [pickerView selectedRowInComponent:0];
        NSDictionary *firstDict = self.pickerDataArray[firstSelectedRow];
        NSArray *secondArray = firstDict[@"cities"];
        return secondArray.count;
    } else {
        NSInteger firstSelectedRow = [pickerView selectedRowInComponent:0];
        NSDictionary *firstDict = self.pickerDataArray[firstSelectedRow];
        NSArray *secondArray = firstDict[@"cities"];
        NSInteger secondSelectedRow = [pickerView selectedRowInComponent:1];
        NSDictionary *secondDict = secondArray[secondSelectedRow];
        NSArray *thirdArray = secondDict[@"counties"];
        return thirdArray.count;
    }
}

#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        NSDictionary *firstDict = self.pickerDataArray[row];
        return firstDict[@"areaName"];
    } else if (component == 1) {
        NSInteger firstSelectedRow = [pickerView selectedRowInComponent:0];
        NSDictionary *firstDict = self.pickerDataArray[firstSelectedRow];
        NSArray *secondArray = firstDict[@"cities"];
        NSDictionary *secondDict = secondArray[row];
        return secondDict[@"areaName"];
    } else {
        NSInteger firstSelectedRow = [pickerView selectedRowInComponent:0];
        NSDictionary *firstDict = self.pickerDataArray[firstSelectedRow];
        NSArray *secondArray = firstDict[@"cities"];
        NSInteger secondSelectedRow = [pickerView selectedRowInComponent:1];
        NSDictionary *secondDict = secondArray[secondSelectedRow];
        NSArray *thirdArray = secondDict[@"counties"];
        NSDictionary *thirdDict = thirdArray[row];
        return thirdDict[@"areaName"];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:false];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:false];
        NSDictionary *firstDict = self.pickerDataArray[row];
        self.pickerViewSelectedShengString = firstDict[@"areaName"];
        NSArray *secondArray = firstDict[@"cities"];
        NSDictionary *secondDict = secondArray.firstObject;
        self.pickerViewSelectedShiString = secondDict[@"areaName"];
        NSArray *thirdArray = secondDict[@"counties"];
        NSDictionary *thirdDict = thirdArray.firstObject;
        self.pickerViewSelectedQuString = thirdDict[@"areaName"];
        self.pickerViewSelectedCode = thirdDict[@"areaId"];
    } else if (component == 1) {
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:false];
        NSInteger firstSelectedRow = [pickerView selectedRowInComponent:0];
        NSDictionary *firstDict = self.pickerDataArray[firstSelectedRow];
        NSArray *secondArray = firstDict[@"cities"];
        NSDictionary *secondDict = secondArray[row];
        self.pickerViewSelectedShiString = secondDict[@"areaName"];
        NSArray *thirdArray = secondDict[@"counties"];
        NSDictionary *thirdDict = thirdArray.firstObject;
        self.pickerViewSelectedQuString = thirdDict[@"areaName"];
        self.pickerViewSelectedCode = thirdDict[@"areaId"];
    } else {
        NSInteger firstSelectedRow = [pickerView selectedRowInComponent:0];
        NSDictionary *firstDict = self.pickerDataArray[firstSelectedRow];
        NSArray *secondArray = firstDict[@"cities"];
        NSInteger secondSelectedRow = [pickerView selectedRowInComponent:1];
        NSDictionary *secondDict = secondArray[secondSelectedRow];
        NSArray *thirdArray = secondDict[@"counties"];
        NSDictionary *thirdDict = thirdArray[row];
        self.pickerViewSelectedQuString = thirdDict[@"areaName"];
        self.pickerViewSelectedCode = thirdDict[@"areaId"];
    }
}

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}

- (NSArray *)pickerDataArray {
    if (!_pickerDataArray) {
        NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"city" ofType:@"json"]];
        _pickerDataArray = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *firstDict = _pickerDataArray.firstObject;
        self.pickerViewSelectedShengString = firstDict[@"areaName"];
        NSArray *secondArray = firstDict[@"cities"];
        NSDictionary *secondDict = secondArray.firstObject;
        self.pickerViewSelectedShiString = secondDict[@"areaName"];
        NSArray *thirdArray = secondDict[@"counties"];
        NSDictionary *thirdDict = thirdArray.firstObject;
        self.pickerViewSelectedQuString = thirdDict[@"areaName"];
        self.pickerViewSelectedCode = thirdDict[@"areaId"];
    }
    return _pickerDataArray;
}

@end
